#!/data/data/com.termux/files/usr/bin/bash
if [ ! -d "test" ]; then
if [ ! -d "ubuntu22.04" ]; then
    termux-setup-storage
    echo "正在更换软件源..."
    sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-main stable main@' $PREFIX/etc/apt/sources.list
    pkg update -y && pkg upgrade -y
    if ! [ -x "$(command -v tar)" ]; then
    pkg install -y tar
    fi
    if ! [ -x "$(command -v pv)" ]; then
    pkg install -y pv
    fi
    if ! [ -x "$(command -v aria2c)" ]; then
    pkg install -y aria2
    fi
    if ! [ -x "$(command -v proot)" ]; then
    apt install proot -y
    fi
    if ! [ -x "$(command -v git)" ]; then
    apt install git -y
    fi
    if ! [ -x "$(command -v curl)" ]; then
    apt install curl -y
    fi
    case $(uname -m) in
    aarch64)
      archtype="arm64"
      ;;
    arm64)
      archtype="arm64"
      ;;
    armv8a)
      archtype="arm64"
      ;;
    arm)
      archtype="armhf"
      ;;
    armv7l)
      archtype="armhf"
      ;;
    armhf)
      archtype="armhf"
      ;;
    amd64)
      archtype="amd64"
      ;;
    x86_64)
      archtype="amd64"
      ;;
    s390x)
      archtype="s390x"
      ;;
    ppc64el)
      archtype="ppc64el"
      ;;
    *)
      echo "不支持的架构 $(uname -m) 镜像站的ubuntu-base可能没有对应你设备的包"
      exit 1
      ;;
    esac
    mkdir test
    echo "正在写入启动命令"
    if [ ! -f ".bashrc" ]; then
        echo echo 启动Ubuntu请输入u > .bashrc
    fi
    if [ ! -f "$PREFIX/bin/u" ]; then
        echo echo 正在启动Ubuntu系统 > $PREFIX/bin/u
        sed -i -e "1a cd ubuntu22.04" $PREFIX/bin/u
        sed -i -e "2a ./startubuntu.sh" $PREFIX/bin/u
        chmod 777 $PREFIX/bin/u
    fi
        echo "正在通过码云安装Ubuntu22.04"
        git clone --depth=1 -b master https://gitee.com/xrk114514/ubuntu22.04.git
        cd ~
        cd ubuntu22.04
        chmod +x ubuntu.sh
        ./ubuntu.sh -y
        echo "bash .xrk" >> /data/data/com.termux/files/home/ubuntu22.04/ubuntu-fs/root/.profile
        curl -o /data/data/com.termux/files/home/ubuntu22.04/ubuntu-fs/root/.xrk https://gitee.com/memories-of-summer/xrk/raw/master/.xrk
        ./startubuntu.sh
    else
        echo "正在打开Ubuntu"
        if [ -d ".bashrc" ]; then
            echo "正在写入启动命令"
            echo echo 启动Ubuntu请输入 u > .bashrc
        fi
        if [ -d "$PREFIX/bin/u" ]; then
            echo echo 正在启动Ubuntu系统 > $PREFIX/bin/u
            sed -i -e "1a cd ubuntu22.04" $PREFIX/bin/u
            sed -i -e "2a ./startubuntu.sh" $PREFIX/bin/u
            chmod 777 $PREFIX/bin/u
        fi
        cd ubuntu22.04 && ./startubuntu.sh
fi
fi
